<?php

namespace Biz\Mailers\Site;

use Biz\Mailers\Mailer;
use Biz\Models\App\AppSite;
use Biz\Models\Site\SiteEmail;
use Lumen\View\Facades\EmailView;
use Illuminate\Support\Facades\Log;
use Biz\Constants\ContainerConstants;
use Biz\Criteria\Site\SiteFilterCriterion;
use Biz\Criteria\Site\TypeFilterCriterion;
use Biz\Criteria\Site\LocaleFilterCriterion;
use Biz\Repositories\Site\SiteEmailRepository;

/**
 * SiteMailer.
 *
 * @license [http://www.85do.com] [杭州永奥网络科技有限公司]
 * @copyright Copyright (c) 2018-2026 Hangzhou Yongao Technology Co., Ltd. All rights reserved.
 */
class SiteMailer extends Mailer
{
    /**
     * AppSite.
     *
     * @var \Biz\Models\App\AppSite
     */
    protected $appSite;

    /**
     * Email.
     *
     * @var \Biz\Models\Site\SiteEmail
     */
    protected $siteEmail;

    /**
     * Email Type.
     *
     * @var string
     */
    protected $emailType;

    /**
     * SiteMailer constructor.
     */
    public function __construct()
    {
        $this->boot();
    }

    /**
     * Boot.
     */
    public function boot()
    {
        $this->initAppSite();
        $this->initSiteEmail();
    }

    /**
     * Init Site.
     */
    public function initAppSite()
    {
        if (app('app')->has(ContainerConstants::BIZ_APP_SITE)) {
            $this->appSite = app(ContainerConstants::BIZ_APP_SITE);
            //Log::debug('SiteMailer InitSite Success');
        }
    }

    /**
     * Init Site Email.
     */
    public function initSiteEmail()
    {
        if ($this->appSite instanceof AppSite) {
            $repository = new SiteEmailRepository();
            $siteEmail  = $repository
                ->pushCriterion(new SiteFilterCriterion($this->appSite->id))
                ->pushCriterion(new TypeFilterCriterion($this->emailType))
                ->pushCriterion(new LocaleFilterCriterion($this->locale))
                ->findFirst();
            if ($siteEmail instanceof SiteEmail) {
                $this->siteEmail = $siteEmail;
                Log::debug('SiteMailer InitSiteEmail Success');
            }
        }
    }

    /**
     * Get App Site Modules.
     */
    public function getAppSite()
    {
        return $this->appSite;
    }

    /**
     * 使用系统默认设置发送邮件.
     */
    public function sendBySystemDefault()
    {
        //Log::debug('SiteMailer-DEFAULT');

        $this->subject($this->subject)
            ->markdown($this->view)
            ->with($this->getTags());

        //Log::debug('SiteMailer-DEFAULT-TO:'.json_encode($this->to));
    }

    /**
     * 使用客户自定义模板发送邮件.
     */
    public function sendByUserCustom(): void
    {
        //Log::debug('SiteMailer-SITE');

        $body = EmailView::make($this->siteEmail->content)
            ->with($this->getTags())
            ->render();

        $this->subject($this->siteEmail->title)
            ->html($body);

        //Log::debug('SiteMailer-SITE-TO:'.json_encode($this->to));
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        if ($this->siteEmail) {
            $this->sendByUserCustom();
        } else {
            $this->sendBySystemDefault();
        }
    }
}
